package com.sbck.voiceclient2;

import android.graphics.Color;
import android.util.Log;
import com.sbck.voiceclient2.myclass.MessageType;
import com.sbck.voiceclient2.myclass.MessageCategory;
import org.greenrobot.eventbus.EventBus;
import com.sbck.voiceclient2.myclass.MyMessage;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SocketUtil {
    private Socket cSocket;
    private ExecutorService cExecutorService;

    public RecvDataThread cRecvDataThread;

    public SocketUtil()
    {
        cExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    }

    @Override
    protected void finalize()
    {
    }
    public void startClient()
    {
        Log.i("tcp", "启动客户端");
        if(cSocket == null)
        {
            Runnable runnable = new Runnable()
            {
                @Override
                public void run()
                {
                    String ip="";
                    try
                    {
                        Log.i("tcp", "启动客户端");

                        ip = "192.168.0.119";
                        cSocket = new Socket(ip, MyGlobal.ServerPort);


                        cSocket.setSoTimeout(1000);
                        Log.i("tcp", "客户端连接成功");
                        SendSocketData("hello bxl!!! ");
                        SendSocketData("你好 !!! ");
                        MyGlobal.IsNetConnected = true;

                        cRecvDataThread = new RecvDataThread(cSocket);
                        cRecvDataThread.start();

                        Thread.sleep(1000);

                        InputStreamReader inputStream =  new InputStreamReader(cSocket.getInputStream(),"GBK");

                        char[] buff = new char[1024];
                        int len = -1;

                        while ((len = inputStream.read(buff)) != -1)
                        {
                            Log.i(getClass().getName(),"收到数据长度:="+len );
//                            Log.i("tcp",data);
                        }
                    }
                    catch (SocketTimeoutException ee)
                    {
                        Log.i("rqp-SocketUtil-","socket读超时异常" + ee.getMessage());
                    }
                    catch (Exception ee)
                    {
                        ee.printStackTrace();
                        Log.i("tcp","Socket异常:" + ee.getMessage());

                        EventBus.getDefault().post(MessageType.NetNotConnect);
                    }
                    finally {

                    }
                }
            };
            cExecutorService.execute(runnable);
        }
    }

    public void Close() {
        Log.i("rqp-SocketUtil-Close()","Socket关闭");
        try {
            if (cSocket != null) {
                cSocket.close();
                cSocket = null;
            }
        } catch (IOException ee) {
            ee.printStackTrace();
        }

        if(cRecvDataThread != null) {
            if (!cRecvDataThread.isInterrupted()) {
                cRecvDataThread.interrupt();
            }
        }
    }

    //发送命令
    public void SendSocketData(String msg)
    {
        MyMessage myMessage = new MyMessage(MessageCategory.SocketSend);

        EventBus.getDefault().post(myMessage);

        if(cSocket != null &&  cSocket.isConnected())
        {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {

                    try
                    {
                        cSocket.getOutputStream().write(msg.getBytes("GBK"));
                        cSocket.getOutputStream().flush();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            };
            cExecutorService.execute(runnable);
        }
    }
}
